package app.maker.ticket.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import app.maker.entity.Ticket;
import app.maker.error.TicketException;
import app.maker.ticket.intf.ITicket;

@Stateless(name = "daoTicket")
public class TicketImpl implements ITicket {

	@PersistenceContext
	private EntityManager em;
	
	public void updateTicket(Ticket ticket) throws TicketException {
		if (ticket != null)
			em.merge(ticket);
		else
			throw new TicketException("Ticket inexistant!");
	}

	public void deleteTicket(Ticket ticket) throws TicketException {
		if (ticket != null)
			em.remove(ticket);
		else
			throw new TicketException("Ticket inexistant!");
	}

	public Ticket getTicket(int id) throws TicketException {
		if (id > 0)
			return em.find(Ticket.class, id);
		else
			throw new TicketException("Identifiant incorrect!");
	}

	@SuppressWarnings("unchecked")
	public List<Ticket> getAllTicket() {
		return em.createNamedQuery("Ticket.findAll").getResultList();
	}

}
